<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\service;

class Group extends \suifeng\mvc\Service
{
    /**
     * 获取群组列表，支持缓存。
     *
     * @return array
     */
    public function get_group_list()
    {
        $list = cache('admin_group_service::get_all');

        if (!$list || APP_DEBUG) {
            $where['status'] = array('eq', 1);
            $order = 'sort asc, id asc';

            $rows = model('admin/group')->where($where)
                ->order($order)
                ->select();

            $list = array();
            foreach ($rows as $row) {
                $row['menu_auth'] = $row['menu_auth'] ? json_decode($row['menu_auth']) : array();
                $row['admin_roles'] = $row['admin_roles'] ? json_decode($row['admin_roles']) : array();
                $row['admin_auth'] = $row['admin_auth'] ? json_decode($row['admin_auth']) : array();
                $list[$row['name']] = $row->getData();
            }

            cache('admin_group_service::get_all', $list);
        }
        return $list;
    }

    /**
     * 获取群组信息。
     *
     * @param unknown $key
     * @return unknown|NULL
     */
    public function get_group_info($key)
    {
        foreach ($this->get_group_list() as $group_info) {
            if ($group_info['id'] == $key || $group_info['name'] == $key) {
                return $group_info;
            }
        }
        return null;
    }

    /**
     * 清除缓存。
     */
    public function clear_cache()
    {
        cache('admin_group_service::get_all', null);
    }

    /**
     * 获取下拉选择项目。
     *
     * @return unknown[]
     */
    public function get_select_options()
    {
        $options = array();
        foreach ($this->get_all() as $row) {
            $options[$row['id']] = $row['name'];
        }
        return $options;
    }
}
